//
//  ChooseStreetViewController.m
//  HZHome
//
//  Created by SunShine on 13-10-14.
//  Copyright (c) 2013年 xzysun. All rights reserved.
//

#import "ChooseStreetViewController.h"
#import "StreetInfoItem.h"
#import "StreetInfoCell.h"
#import "StreetInfoService.h"

@interface ChooseStreetViewController ()

@end

@implementation ChooseStreetViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    if (self.usedForPersonalInfo) {
        self.title = @"选择街道";
    } else {
        [self startLoadingView];
        self.title = @"请选择关注的街道";
        _streetListService = [[CommitteesByStreetService alloc]init];
        _streetListService.delegate = self;
        [_streetListService loadDataWithKey:nil];
        [UserInfoService getUserInfoService].delegate = self;
        NSString *storedId = [DataStoreService loadFromDefaults:CURRENT_SELECTED_STREET_ID_KEY];
        if (storedId) {
            //读取本地存储的值
            self.selectedStreetId = storedId.integerValue;
        } else {
            //如果没有值则初始化为-1
            self.selectedStreetId = -1;
        }
    }
    self.navigationItem.leftBarButtonItem = autoavBackButton(self, @selector(backMethod));

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Data Service Method
-(void)loadDataFinished
{
    [self.tableView reloadData];
    [self stopLoadingView];
}

-(void)loadDataFaild:(NSString *)msg
{
    [self stopLoadingView];
}

-(void)setEnableToLoadMore:(BOOL)enable
{
    //did nothing
}

#pragma mark - Table Method
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section==0) {
        return _streetListService.dataList?[_streetListService.dataList count]:0;
    } else {
        return 0;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    StreetInfoCell *cell = [[[NSBundle mainBundle]loadNibNamed:@"StreetInfoCell" owner:self options:nil]objectAtIndex:0];
    StreetInfoItem *item =[_streetListService.dataList objectAtIndex:indexPath.row];
    cell.titleLabel.text = item.item_name;
    if (item.item_id.integerValue == self.selectedStreetId) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    } else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    return cell;
}
//列表事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    StreetInfoItem *item =[_streetListService.dataList objectAtIndex:indexPath.row];
    self.selectedStreetId = item.item_id.integerValue;
    [tableView reloadData];
    if (self.usedForPersonalInfo) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(choosedStreet:)]) {
            //保存数据
            [DataStoreService saveToDefaults:CURRENT_SELECTED_STREET_ID_KEY data:item.item_id];
            [DataStoreService saveToDefaults:CURRENT_SELECTED_STREET_NAME_KEY data:item.item_name];
            [StreetInfoService getService].selectedStreetId = item.item_id;
             //保存数据
            [self.delegate choosedStreet:self.selectedStreetId];
            [self.navigationController popViewControllerAnimated:YES];
        }
    } else {
        [self startLoadingViewWithText:@"正在保存..."];
        //发起请求
        [[UserInfoService getUserInfoService] customiezStreetWithStreetId:item.item_id];
    } 
}
//定制街道 成功
#pragma mark - User Info Service Delegate
-(void)loadUserInfoFinished:(id)result
{
    for (StreetInfoItem *item in _streetListService.dataList) {
        if (self.selectedStreetId == item.item_id.integerValue) {
            //保存数据
            [DataStoreService saveToDefaults:CURRENT_SELECTED_STREET_ID_KEY data:item.item_id];
            [DataStoreService saveToDefaults:CURRENT_SELECTED_STREET_NAME_KEY data:item.item_name];
            [StreetInfoService getService].selectedStreetId = item.item_id;
            break;
        }
    }
    [self stopLoadingView];
    //移除最后的视图，后退
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)loadUserInfoFaild:(NSString *)msg
{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:msg delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
    [self stopLoadingView];
}
@end
